﻿using System;
using System.Dynamic;
using Facebook;
using TwitterVB2;
using WLMCoreLibrary;

namespace MiWebSite_Core
{
    public class Twitter
    {
        public string ConsumerKey
        {
            get;
            set;
        }

        public string ConsumerKeySecret
        {
            get;
            set;
        }

        public string Token
        {
            get;
            set;
        }

        public string TokenSecret
        {
            get;
            set;
        }

        public Twitter()
        {
            //rBASq4rylut5gD9UleUq7w
            ConsumerKey = "rBASq4rylut5gD9UleUq7w";

            //PWma8C0xWHtcQyE1ej9JtzbcIWNzlnCXGOq2GrOV8
            ConsumerKeySecret = "PWma8C0xWHtcQyE1ej9JtzbcIWNzlnCXGOq2GrOV8";
            if ( CurrentDomain.oauth_token != "" )
            {
                Token = CurrentDomain.oauth_token;
                TokenSecret = CurrentDomain.oauth_token_secret;
            }
        }

        public Twitter( string AppConsumerKey, string AppConsumerKeySecret, string UserToken, string UserTokenSecret )
        {
            ConsumerKey = AppConsumerKey;
            ConsumerKeySecret = AppConsumerKeySecret;
            Token = UserToken;
            TokenSecret = UserTokenSecret;
        }

        public void Update( string statusUpdate )
        {
            try
            {
                TwitterAPI tw = new TwitterAPI();
                tw.AuthenticateWith( ConsumerKey, ConsumerKeySecret, Token, TokenSecret );

                if ( statusUpdate.Length > 140 )
                    statusUpdate = statusUpdate.Substring( 0, 140 );
                TwitterStatus status = tw.Update( statusUpdate );
            }
            catch ( Exception ex )
            {
                if ( System.Web.HttpContext.Current.IsDebuggingEnabled )
                {
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.ContentType = "txt/html";
                    System.Web.HttpContext.Current.Response.Write( ex.Message + "<br><br>" + ex.StackTrace );
                    System.Web.HttpContext.Current.Response.End();
                }
            }
        }

        public string AutorizarTwit( string oauth_token, string oauth_verifier, MiWebSiteDomain dominio )
        {
            TwitterAPI tw = new TwitterAPI();

            // Exchange the rquest token for an access token

            tw.GetAccessTokens( ConsumerKey, ConsumerKeySecret, oauth_token, oauth_verifier );
            dominio.oauth_token = tw.OAuth_Token;
            dominio.oauth_token_secret = tw.OAuth_TokenSecret;
            dominio.Save();
            Token = tw.OAuth_Token;
            TokenSecret = tw.OAuth_TokenSecret;

            Update( "Aplicacion MiWebsite '" + dominio.dominio + "' Correctamente Autorizada" );
            return tw.AccountInformation().ScreenName;
        }

        public string RequestAuthorization( MiWebSiteDomain dominio )
        {
            TwitterAPI tw = new TwitterAPI();
            string CallbackURL="http://" + dominio.dominio + "/authorizeTwitter/";
            return tw.GetAuthorizationLink( ConsumerKey, ConsumerKeySecret, CallbackURL );
        }

        public string RequestAuthorization( MiWebSiteDomain dominio, string retunFolder )
        {
            TwitterAPI tw = new TwitterAPI();
            return tw.GetAuthorizationLink( ConsumerKey, ConsumerKeySecret, "http://" + dominio.dominio + "/" + retunFolder + "/" );
        }
    }

    public class FacebookPost
    {
        public string AccessToken { get; set; }

        public FacebookPost()
        {
            AccessToken = CurrentDomain.FacebookAccessToken;
        }

        public void Post( string messageTitle, string linkURL, string pictureURL, string messageName, string caption, string description )
        {
            FacebookClient client = new Facebook.FacebookClient( AccessToken );
            dynamic parameters= new ExpandoObject();
            parameters.message = messageTitle;
            parameters.link = linkURL;
            parameters.picture = pictureURL;
            parameters.name = messageName;
            parameters.caption = caption;
            parameters.description = description;
            parameters.actions = new
            {
                name = "ver",
                link = linkURL,
            };
            parameters.privacy = new
            {
                value = "ALL_FRIENDS",
            };
            parameters.targeting = new
            {
                countries = "US",
                regions = "6,53",
                locales = "6",
            };
            dynamic result = client.Post( "me/feed", parameters );
        }

        public void PostToPage( string PagePath, string messageTitle, string linkURL, string pictureURL, string messageName, string caption, string description )
        {
            FacebookClient client = new Facebook.FacebookClient( AccessToken );

            dynamic parameters= new ExpandoObject();
            parameters.message = messageTitle;
            parameters.link = linkURL;
            parameters.picture = pictureURL;
            parameters.name = messageName;
            parameters.caption = caption;
            parameters.description = description;
            parameters.from = PagePath;
            parameters.to = "150915905021536";
            parameters.actions = new
            {
                name = "ver",
                link = linkURL,
            };

            //parameters.privacy = new
            //{
            //    value = "ALL_FRIENDS",
            //};
            parameters.targeting = new
            {
                countries = "US",
                regions = "6,53",
                locales = "6",
            };

            dynamic result = client.Post( "/" + PagePath + "/feed", parameters );
        }
    }
}